package com.jhgsys.internal.hazard.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.hazard.entity.HazardExamineModuleConfig;
import com.jhgsys.internal.hazard.entity.HazardModuleConfig;
import com.jhgsys.internal.hazard.service.IHazardExamineModuleConfigService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 *  Controller
 *
 * @author yangjie
 * @date 2024-09-10 17:19:58
 */
@Slf4j
@Validated
@Controller
public class HazardExamineModuleConfigController extends BaseController {

    @Autowired
    private IHazardExamineModuleConfigService hazardExamineModuleConfigService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardExamineModuleConfig")
    @RequiresPermissions("hazardExamineModuleConfig:view")
    public String hazardExamineModuleConfigIndex(Model model){
        model.addAttribute("tenantList", getTenantList());
        model.addAttribute("tenantId", getCurrentTenantId());
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        if(StringUtils.isNotBlank(getCurrentTenantId())){
            HazardExamineModuleConfig hazardExamineModuleConfig = hazardExamineModuleConfigService.findHazardExamineModuleConfigByTenantId(getCurrentTenantId());
            if(hazardExamineModuleConfig!=null){
                model.addAttribute("hazardExamineModuleConfig",hazardExamineModuleConfig);
            }
        }
        return FebsUtil.view("hazard/hazardExamineModuleConfig/hazardExamineModuleConfigList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardExamineModuleConfig/add")
    @RequiresPermissions("hazardExamineModuleConfig:add")
    public String hazardExamineModuleConfigAdd(){
        return FebsUtil.view("hazard/hazardExamineModuleConfig/hazardExamineModuleConfigEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardExamineModuleConfig/update/{id}")
    @RequiresPermissions("hazardExamineModuleConfig:edit")
    public String hazardExamineModuleConfigUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("hazardExamineModuleConfig", hazardExamineModuleConfigService.findHazardExamineModuleConfigById(id));
        return FebsUtil.view("hazard/hazardExamineModuleConfig/hazardExamineModuleConfigEdit");
    }

    /**
     * 查询（单个）
     * @param model
     * @param id
     * @return
     */
    @GetMapping("hazardExamineModuleConfig/search/{id}")
    @ResponseBody
    @RequiresPermissions("hazardModuleConfig:view")
    public FebsResponse getHazardModuleConfigById(Model model, @PathVariable("id") String  id) {
        HazardExamineModuleConfig hazardExamineModuleConfig = hazardExamineModuleConfigService.findHazardExamineModuleConfigByTenantId(id);
        model.addAttribute("hazardExamineModuleConfig", hazardExamineModuleConfig);
        return new FebsResponse().success().data(model);
    }

    /**
     * 查询（所有）
     * @author yangjie
     * @date 2024-09-10 17:19:58
     */
    @GetMapping("hazardExamineModuleConfig")
    @ResponseBody
    @RequiresPermissions("hazardExamineModuleConfig:view")
    public FebsResponse getAllHazardExamineModuleConfigs(HazardExamineModuleConfig hazardExamineModuleConfig) {
        return new FebsResponse().success().data(hazardExamineModuleConfigService.findHazardExamineModuleConfigList(hazardExamineModuleConfig));
    }

    /**
     * 查询（分页）
     * @author yangjie
     * @date 2024-09-10 17:19:58
     */
    @GetMapping("hazardExamineModuleConfig/list")
    @ResponseBody
    @RequiresPermissions("hazardExamineModuleConfig:view")
    public FebsResponse hazardExamineModuleConfigList(QueryRequest request, HazardExamineModuleConfig hazardExamineModuleConfig) {
        Map<String, Object> dataTable = getDataTable(this.hazardExamineModuleConfigService.findHazardExamineModuleConfigPage(request, hazardExamineModuleConfig));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 HazardExamineModuleConfig
     * @author yangjie
     * @date 2024-09-10 17:19:58
     */
    @Log("新增HazardExamineModuleConfig")
    @PostMapping("hazardExamineModuleConfig")
    @ResponseBody
    @RequiresPermissions("hazardExamineModuleConfig:edit")
    public FebsResponse addHazardExamineModuleConfig(@RequestBody HazardExamineModuleConfig hazardExamineModuleConfig) throws FebsException {
        try {
            hazardExamineModuleConfig.setCreatedBy(getCurrentUser().getUserName());
            this.hazardExamineModuleConfigService.createHazardExamineModuleConfig(hazardExamineModuleConfig);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增HazardExamineModuleConfig失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 逻辑删除 HazardExamineModuleConfig
     * @author yangjie
     * @date 2024-09-10 17:19:58
     */
    @Log("逻辑删除HazardExamineModuleConfig")
    @GetMapping("hazardExamineModuleConfig/delete/{id}")
    @ResponseBody
    @RequiresPermissions("hazardExamineModuleConfig:delete")
    public FebsResponse deleteHazardExamineModuleConfig(@PathVariable("id") String hazardExamineModuleConfigIds) throws FebsException {
        try {
            HazardExamineModuleConfig hazardExamineModuleConfig = new HazardExamineModuleConfig();
            hazardExamineModuleConfig.setDeletedBy(getCurrentUser().getUserName());
            hazardExamineModuleConfig.setDeletedTime(new Date());
            hazardExamineModuleConfig.setDeletedFlg(FebsConstant.INVALID);
            this.hazardExamineModuleConfigService.deleteHazardExamineModuleConfig(hazardExamineModuleConfig,hazardExamineModuleConfigIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除HazardExamineModuleConfig失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
    * 物理删除 HazardExamineModuleConfig
    * @author yangjie
    * @date 2024-09-10 17:19:58
    */
    @Log("物理删除HazardExamineModuleConfig")
    @GetMapping("hazardExamineModuleConfig/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("hazardExamineModuleConfig:delete")
    public FebsResponse physicallyDeleteHazardExamineModuleConfig(@PathVariable("id") String hazardExamineModuleConfigIds) throws FebsException {
        try {
            this.hazardExamineModuleConfigService.physicallyDeleteHazardExamineModuleConfig(hazardExamineModuleConfigIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除HazardExamineModuleConfig失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 修改 HazardExamineModuleConfig
     * @author yangjie
     * @date 2024-09-10 17:19:58
     */
    @Log("修改HazardExamineModuleConfig")
    @PostMapping("hazardExamineModuleConfig/update")
    @ResponseBody
    @RequiresPermissions("hazardExamineModuleConfig:edit")
    public FebsResponse updateHazardExamineModuleConfig(@RequestBody HazardExamineModuleConfig hazardExamineModuleConfig) throws FebsException {
        try {
            hazardExamineModuleConfig.setUpdatedBy(getCurrentUser().getUserName());
            this.hazardExamineModuleConfigService.updateHazardExamineModuleConfig(hazardExamineModuleConfig);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改HazardExamineModuleConfig失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

}
